home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / MENU95.PAK / OWNRDRAW.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.6 KB  |  286 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: ownrdraw.c
  9. //
  10. //  PURPOSE: Example of a menu with simple owner draw items.
  11. //
  12. //
  13. //  FUNCTIONS:
  14. //    MsgDrawItem - Handles updating of owner draw menu item.
  15. //    CmdColors - Toggle owner draw state of color menu.
  16. //    CdmColorOwnerDr - Handle checking/unchecking of colors.
  17. //
  18. //  COMMENTS:
  19. //
  20.  
  21. #include <windows.h>            // required for all Windows applications
  22. #include "globals.h"            // prototypes specific to this application
  23. #include "resource.h"
  24.  
  25. static UINT idmCur = IDM_BLACK;
  26.  
  27. #define MEASUREITEMWIDTH  40
  28. #define MEASUREITEMHEIGHT 40
  29.  
  30. #pragma argsused
  31. LRESULT MsgMeasureItem(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  32. {
  33.     LPMEASUREITEMSTRUCT lpmi = (LPMEASUREITEMSTRUCT)lparam;
  34.  
  35.     // Use the same width for all items. We could examine the item id
  36.     // and use different widths/heights for each item.
  37.  
  38.     lpmi->itemWidth  = MEASUREITEMWIDTH;
  39.     lpmi->itemHeight = MEASUREITEMHEIGHT;
  40.  
  41.     return TRUE;
  42. }
  43.  
  44. //
  45. //  FUNCTION: MsgDrawItem(HWND, UINT, WPARAM, LPARAM)
  46. //
  47. //  PURPOSE: Handles updating of owner draw menu item.
  48. //
  49. //  PARAMETERS:
  50. //    hwnd      - Window handle  (Unused)
  51. //    uMessage  - WM_DRAWITEM    (Unused)
  52. //    wparam    - Extra data     (Unused)
  53. //    lparam    - LPDRAWITEMSTRUCT
  54. //
  55. //  RETURN VALUE:
  56. //    Always returns 0 - Message handled
  57. //
  58. //  COMMENTS:
  59. //    Called in response to a WM_DRAWITEM message, i.e. when the
  60. //    colors menu is being modified to an owner-draw menu, or
  61. //    one of the items is selected. It sizes the checkmark bitmap
  62. //    to fit next to a color band and draws the color bands and
  63. //    the checkmark on the popup menu.
  64. //
  65. //
  66.  
  67. #pragma argsused
  68. LRESULT MsgDrawItem(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
  69. {
  70.     LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT) lparam;
  71.     HBRUSH  hbr;
  72.     RECT    rc;
  73.     LONG    lSize;
  74.     int     hSize;
  75.     int     vSize;
  76.  
  77.     // Get the size of the checkmark so we can leave room for it since we
  78.     // want to be able to check the selected color.
  79.  
  80.     lSize = GetMenuCheckMarkDimensions();
  81.     vSize = HIWORD(lSize);
  82.     hSize = LOWORD(lSize);
  83.  
  84.     if (lpdis->itemAction == ODA_SELECT ||
  85.         lpdis->itemAction == ODA_DRAWENTIRE)
  86.     {
  87.         CopyRect(&rc, &lpdis->rcItem);
  88.         InflateRect(&rc, -(hSize+2), -2);
  89.  
  90.         if (lpdis->itemState & ODS_SELECTED)
  91.         {
  92.                 // Item has been selected -- hilite with a gray frame
  93.             hbr = GetStockObject(GRAY_BRUSH);
  94.             FrameRect(lpdis->hDC, &rc, hbr);
  95.         }
  96.         else if (lpdis->itemAction == ODA_SELECT)
  97.         {
  98.             // Item has been de-selected -- remove gray frame
  99.             hbr = CreateSolidBrush(GetSysColor(COLOR_MENU));
  100.             FrameRect(lpdis->hDC, &rc, hbr);
  101.             DeleteObject(hbr);
  102.         }
  103.     }
  104.  
  105.      if (lpdis->itemAction == ODA_DRAWENTIRE)
  106.     {
  107.         // Paint the color item in the color requested.
  108.         hbr = CreateSolidBrush(lpdis->itemData);
  109.         CopyRect(&rc, &lpdis->rcItem);
  110.         InflateRect(&rc, -(hSize+10), -10);
  111.         FillRect(lpdis->hDC, (LPRECT)&rc, hbr);
  112.         DeleteObject(hbr);
  113.  
  114.         if (lpdis->itemState & ODS_CHECKED)
  115.         {
  116.             HDC     hdcBitmap;
  117.             HBITMAP hbmpSave;
  118.                 DWORD   textColorSave;
  119.             DWORD   bkColorSave;
  120.  
  121.             // Draw the check mark if the item is checked.
  122.             hdcBitmap = CreateCompatibleDC(lpdis->hDC);
  123.             hbmpSave  = SelectObject(hdcBitmap, hbmpDot);
  124.             textColorSave = SetTextColor(lpdis->hDC, 0x00000000L);
  125.             bkColorSave   = SetBkColor(lpdis->hDC, 0x00FFFFFFL);
  126.  
  127.             BitBlt(lpdis->hDC,
  128.                    lpdis->rcItem.left,
  129.                    lpdis->rcItem.top + (MEASUREITEMHEIGHT - vSize) / 2,
  130.                    hSize,
  131.                          vSize,
  132.                    hdcBitmap,
  133.                    0, 0,
  134.                    SRCAND);
  135.  
  136.             // Restore colors and bitmap and clean up
  137.             SetTextColor(lpdis->hDC, textColorSave);
  138.             SetBkColor(lpdis->hDC, bkColorSave);
  139.             SelectObject(hdcBitmap, hbmpSave);
  140.             DeleteDC(hdcBitmap);
  141.         }
  142.     }
  143.  
  144.      return TRUE;
  145. }
  146.  
  147.  
  148. //
  149. //  FUNCTION: CmdColors(HWND, WORD, WORD, HWND)
  150. //
  151. //  PURPOSE: Toggle owner draw state of color menu.
  152. //
  153. //  PARAMETERS:
  154. //    hwnd      - Window handle
  155. //    wCommand  - WM_BLACK, WM_RED, WM_BLUE, or WM_GREEN
  156. //    wNotify   - Notification number (unused)
  157. //    hwndCtrl  - NULL.
  158. //
  159. //  RETURN VALUE:
  160. //    Always returns 0 - Message handled
  161. //
  162. //  COMMENTS:
  163. //
  164. //
  165.  
  166. #pragma argsused
  167. LRESULT CmdColors(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  168. {
  169.     if ((UINT)wCommand != idmCur)
  170.     {
  171.         // Get the Color Menu from the window
  172.         HMENU hmenu = GetSubMenu(GetMenu(hwnd), IDCOLORS_POS);
  173.  
  174.         MENUITEMINFO mii;
  175.  
  176.         // Uncheck the currently selected item
  177.         mii.cbSize = sizeof(MENUITEMINFO);
  178.         mii.fMask  = MIIM_STATE;
  179.         mii.fState = MFS_UNCHECKED;
  180.         SetMenuItemInfo(hmenu, idmCur, FALSE, &mii);
  181.  
  182.         // Check the newly checked item
  183.         mii.fState = MFS_CHECKED;
  184.         SetMenuItemInfo(hmenu, wCommand, FALSE, &mii);
  185.  
  186.         idmCur = (UINT)wCommand;
  187.     }
  188.  
  189.     return 0;
  190. }
  191.  
  192.  
  193. //
  194. //  FUNCTION: CmdColorOwnerDr(HWND, WORD, WORD, HWND)
  195. //
  196. //  PURPOSE: Handle checking/unchecking of colors.
  197. //
  198. //  PARAMETERS:
  199. //    hwnd      - Window handle
  200. //    wCommand  - IDM_COLOROWNERDR    (Unused)
  201. //    wNotify   - Notification number (unused)
  202. //    hwndCtrl  - NULL.               (Unused)
  203. //
  204. //  RETURN VALUE:
  205. //
  206. //    Always returns 0 - Message handled
  207. //
  208. //  COMMENTS:
  209. //    Toggles the state of the Owner Draw item in the Colors menu. If it
  210. //    is on, the "Black", "Blue", "Red", and "Green" individual menu text
  211. //    items are modified so that they will contain bands of color.
  212. //    Otherwise, the colors are replaced by the text.
  213. //
  214. //
  215.  
  216. typedef struct _CLRM {
  217.     WORD  wCommand;     // Command number for menu item
  218.     DWORD dwColor;      // RGB color associated with this entry
  219.     UINT  cch;          // Size of the string
  220.     CHAR *szColor;      // Descriptive string for this entry
  221. } CLRM; // Color menu info
  222.  
  223. CLRM rgclrm[] = {
  224.     {IDM_BLACK, RGB(  0,   0,   0), 6, "&Black"},
  225.     {IDM_BLUE,  RGB(  0,   0, 255), 5, "B&lue"},
  226.     {IDM_RED,   RGB(255,   0,   0), 4, "&Red"},
  227.     {IDM_GREEN, RGB(  0, 255,   0), 6, "&Green"}
  228. };
  229.  
  230. #pragma argsused
  231. LRESULT CmdColorOwnerDr(HWND hwnd, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  232. {
  233.     MENUITEMINFO mii;
  234.     HMENU hmenu;
  235.     BOOL  fOwnerDraw;
  236.     int   iclrm;
  237.  
  238.     // Get a handle to the Colors menu. This is at position 1.
  239.     hmenu = GetSubMenu(GetMenu(hwnd), IDCOLORS_POS);
  240.  
  241.     // Get the current state of the item
  242.     mii.cbSize = sizeof(MENUITEMINFO);
  243.      mii.fMask = MIIM_STATE;
  244.     GetMenuItemInfo(hmenu, IDM_COLOROWNERDR, FALSE, &mii);
  245.     fOwnerDraw = !(mii.fState & MFS_CHECKED);
  246.  
  247.     // Toggle the state of the item.
  248.     mii.fState = (fOwnerDraw ? MFS_CHECKED : MFS_UNCHECKED);
  249.     SetMenuItemInfo(hmenu, IDM_COLOROWNERDR, FALSE, &mii);
  250.  
  251.     mii.fType = (fOwnerDraw ? MFT_OWNERDRAW : MFT_STRING);
  252.     mii.fMask = MIIM_STATE | MIIM_TYPE;
  253.  
  254.     for ( iclrm = 0; iclrm < sizeof(rgclrm) / sizeof(CLRM); iclrm++)
  255.     {
  256.           CLRM *pclrm = &rgclrm[iclrm];
  257.  
  258.         // Get the state of the menu item
  259.         mii.fState = (pclrm->wCommand == idmCur) ? MFS_CHECKED : MFS_UNCHECKED;
  260.  
  261.         // if it is an owner draw get its data otherwise get its string
  262.         if (fOwnerDraw)
  263.         {
  264.             // In case of Owner draw the MIIM_DATA should also be set since
  265.             // WM_DRAWITEM will be sent with the value of dwItemData and not
  266.             // dwTypeDate.  The dwTypeData is set also because MIIM_TYPE is
  267.             // passed to update the data type(ie MFT_OWNERDRAW vs MFT_STRING)
  268.             mii.fMask |= MIIM_DATA;
  269.                 mii.dwItemData = pclrm->dwColor;
  270.             mii.dwTypeData = (LPSTR)pclrm->szColor;
  271.             mii.cch = pclrm->cch;
  272.         }
  273.         else
  274.         {
  275.             // update the string values
  276.             mii.dwTypeData = (LPSTR)pclrm->szColor;
  277.             mii.cch = pclrm->cch;
  278.         }
  279.  
  280.         // Update the menu item information structure.
  281.         SetMenuItemInfo(hmenu, pclrm->wCommand, FALSE, &mii);
  282.      }
  283.  
  284.     return TRUE;
  285. }
  286.